import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'
const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/Ledger/EquipType/Index',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'RedirectWrap',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login')
      // noTagsView: true
    }
  },
  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/personal-center',
    name: 'Personal',
    meta: {
      title: t('router.personal'),
      hidden: true,
      canTo: true
    },
    children: [
      {
        path: 'personal-center',
        component: () => import('@/views/Personal/PersonalCenter/PersonalCenter.vue'),
        name: 'PersonalCenter',
        meta: {
          title: t('router.personalCenter'),
          hidden: true,
          canTo: true
        }
      }
    ]
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/Ledger',
    component: Layout,
    name: 'ledger',
    redirect: '/Ledger/EquipType/Index',
    meta: {
      title: '设备设施台账管理',
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'EquipType',
        name: 'equiptype',
        component: getParentLayout(),
        redirect: '/Ledger/EquipType/Index',
        meta: {
          title: t('设施基础管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'Index',
            component: () => import('@/views/EquipManage/EquipType/index.vue'),
            name: 'index',
            meta: {
              title: '设施类型维护',
              noCache: true
            }
          }
        ]
      },
      {
        path: 'LedgerManage',
        name: 'ledgermanage',
        component: getParentLayout(),
        redirect: '/Ledger/LedgerManage/FacilityLedger/FacilityList',
        meta: {
          title: t('台账管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'FacilityLedger',
            name: 'facilityledger',
            component: getParentLayout(),
            redirect: '/Ledger/LedgerManage/FacilityLedger/FacilityList',
            meta: {
              title: t('设施设备台账'),
              alwaysShow: true
            },
            children: [
              {
                path: 'FacilityList',
                component: () => import('@/views/EquipManage/EquipLedger/index.vue'),
                name: 'facilityList',
                meta: {
                  title: '设施台账',
                  noCache: true
                }
              },
              {
                path: 'CutOffFacility',
                component: () => import('@/views/EquipManage/CutOffLedger/index.vue'),
                name: 'CutOfffacility',
                meta: {
                  title: '截止设施台账',
                  noCache: true
                }
              },
              {
                path: 'RedFacility',
                component: () => import('@/views/EquipManage/RedLineLedger/index.vue'),
                name: 'redfacility',
                meta: {
                  title: '红线内设施台账',
                  noCache: true
                }
              }
            ]
          },
          {
            path: 'PipnetLedger',
            component: () => import('@/views/EquipManage/PipnetLedger/index.vue'),
            name: 'pipnetledger',
            meta: {
              title: '管网台账',
              noCache: true
            }
          },
          {
            path: 'PipeLineLedger',
            component: () => import('@/views/EquipManage/PipelineLedger/index.vue'),
            name: 'pipelineledger',
            meta: {
              title: '管线台账',
              noCache: true
            }
          },
          {
            path: 'FacListOut',
            component: () => import('@/views/EquipManage/FacListOut/index.vue'),
            name: 'faclistout',
            meta: {
              title: '设施出厂台账',
              noCache: true
            }
          },
          {
            path: 'DeviceListOut',
            component: () => import('@/views/EquipManage/DeviceListOut/index.vue'),
            name: 'deviceListout',
            meta: {
              title: '设备出厂台账',
              noCache: true
            }
          },
          {
            path: 'ProducerLedger',
            component: () => import('@/views/EquipManage/ProducerLedger/index.vue'),
            name: 'producerledger',
            meta: {
              title: '厂商台账',
              noCache: true
            }
          }
        ]
      }
    ]
  },
  {
    path: '/linePatrol',
    component: Layout,
    redirect: '/linePatrol/ledger',
    name: 'LinePatrol',
    meta: {
      title: t('巡检管理'),
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'ledger',
        component: () => import('@/views/linePatrol/Ledger/index.vue'),
        name: 'Ledger',
        meta: {
          title: t('巡检点管理'),
          noCache: true
        }
      },
      {
        path: 'PatrolScheme',
        name: 'PatrolScheme',
        component: getParentLayout(),
        redirect: '/linePatrol/PatrolScheme/PlanGroup',
        meta: {
          title: t('巡检计划管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'PlanGroup',
            component: () => import('@/views/linePatrol/PlanGroup/index.vue'),
            name: 'PlanGroup',
            meta: {
              title: t('建立巡检组'),
              noCache: true
            }
          },
          {
            path: 'LssuePlan',
            component: () => import('@/views/linePatrol/LssuePlan/index.vue'),
            name: 'LssuePlan',
            meta: {
              title: t('下发巡检计划'),
              noCache: true,
              customParams: {
                // 自定义静态参数
                planType: 'LssuePlan',
                groupUserCom: 3
              }
            }
          },
          {
            path: 'ShowPlanList',
            component: () => import('@/views/linePatrol/LssuePlan/components/plantaskdtl.vue'),
            name: 'ShowPlanList',
            meta: {
              title: '相关巡检计划',
              noTagsView: true,
              noCache: true,
              hidden: true,
              canTo: true,
              activeMenu: '/ShowPlanList'
            }
          }
        ]
      },
      {
        path: 'Inspection',
        name: 'Inspection',
        component: getParentLayout(),
        redirect: '/linePatrol/Inspection/InspectionPerspective',
        meta: {
          title: t('巡检管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'InspectionTrajectory',
            component: () => import('@/views/linePatrol/InspectionTrajectory/index.vue'),
            name: 'InspectionTrajectory',
            meta: {
              title: t('巡检轨迹'),
              noCache: true
            }
          },
          {
            path: 'InspectionTasks',
            component: () => import('@/views/linePatrol/InspectionTasks/index.vue'),
            name: 'InspectionTasks',
            meta: {
              title: t('巡检任务'),
              noCache: true
            }
          }
        ]
      },
      {
        path: 'BasisManage',
        name: 'BasisManage',
        component: getParentLayout(),
        redirect: '/linePatrol/Description/Parameter',
        meta: {
          title: t('巡线基础管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'Parameter',
            component: () => import('@/views/linePatrol/Parameter/index.vue'),
            name: 'Parameter',
            meta: {
              title: t('巡线参数维护'),
              noCache: true
            }
          },
          {
            path: 'Area',
            component: () => import('@/views/linePatrol/Area/index.vue'),
            name: 'Area',
            meta: {
              title: t('巡线片区维护'),
              noCache: true
            }
          },
          {
            path: 'Description',
            component: () => import('@/views/linePatrol/Description/index.vue'),
            name: 'Description',
            meta: {
              title: t('事件类型维护'),
              noCache: true
            }
          }
        ]
      }
    ]
  },
  {
    path: '/SecurCheck',
    component: Layout,
    redirect: '/SecurCheck/LedgerMessage/UserLedger',
    name: 'SecurCheck',
    meta: {
      title: t('安检管理'),
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'LedgerMessage',
        name: 'LedgerMessage',
        component: getParentLayout(),
        // redirect: '/linePatrol/PatrolScheme/PlanGroup',
        meta: {
          title: t('台账管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'UserLedger',
            component: () => import('@/views/SecurCheck/LedgerMessage/UserLedger/index.vue'),
            name: 'UserLedger',
            meta: {
              title: '用户台账',
              noCache: true
            }
          },
          {
            path: 'UnitLedger',
            component: () => import('@/views/SecurCheck/LedgerMessage/UnitLedger/index.vue'),
            name: 'UnitLedger',
            meta: {
              title: '单位台账',
              noCache: true
            }
          },
          {
            path: 'MeterLedger',
            component: () => import('@/views/SecurCheck/LedgerMessage/MeterLedger/index.vue'),
            name: 'MeterLedger',
            meta: {
              title: '表具台账',
              noCache: true
            }
          },
          {
            path: 'FloorLedger',
            component: () => import('@/views/SecurCheck/LedgerMessage/FloorLedger/index.vue'),
            name: 'FloorLedger',
            meta: {
              title: '楼宇台账',
              noCache: true
            }
          },
          {
            path: 'ProducerLedger',
            component: () => import('@/views/SecurCheck/LedgerMessage/ProducerLedger/index.vue'),
            name: 'ProducerLedger',
            meta: {
              title: '厂商台账',
              noCache: true
            }
          },
          {
            path: 'ContractManagement',
            component: () =>
              import('@/views/SecurCheck/LedgerMessage/ContractManagement/index.vue'),
            name: 'ContractManagement',
            meta: {
              title: '工商合同管理',
              noCache: true
            }
          },
          {
            path: 'GasEquipmentLedger',
            component: () =>
              import('@/views/SecurCheck/LedgerMessage/GasEquipmentLedger/index.vue'),
            name: 'GasEquipmentLedger',
            meta: {
              title: '燃气设备台账',
              noCache: true
            }
          }
        ]
      },
      {
        path: 'PlanMessage',
        name: 'PlanMessage',
        component: getParentLayout(),
        // redirect: '/linePatrol/PatrolScheme/PlanGroup',
        meta: {
          title: t('计划管理'),
          alwaysShow: true
        },
        children: [
          {
            path: 'UserGroup',
            component: () => import('@/views/SecurCheck/PlanMessage/UserGroup/index.vue'),
            name: 'UserGroup',
            meta: {
              title: '建立民用户组',
              noCache: true
            }
          },
          {
            path: 'CustomerUnitGroup',
            component: () => import('@/views/SecurCheck/PlanMessage/CustomerUnitGroup/index.vue'),
            name: 'CustomerUnitGroup',
            meta: {
              title: '建立单位户组',
              noCache: true
            }
          },
          {
            path: 'IssueUser',
            component: () => import('@/views/linePatrol/LssuePlan/index.vue'),
            // component: () => import('@/views/SecurCheck/PlanMessage/IssueUser/index.vue'),
            name: 'IssueUser',
            meta: {
              title: '下发民用户客服计划',
              noCache: true,
              customParams: {
                // 自定义静态参数
                planType: 'IssueUser',
                groupUserCom: 0
              }
            }
          },
          {
            path: 'IssueCustomerUnit',
            component: () => import('@/views/linePatrol/LssuePlan/index.vue'),
            // component: () => import('@/views/SecurCheck/PlanMessage/IssueCustomer/index.vue'),
            name: 'IssueCustomerUnit',
            meta: {
              title: '下发单位客服计划',
              noCache: true,
              customParams: {
                // 自定义静态参数
                planType: 'IssueCustomerUnit',
                groupUserCom: 1
              }
            }
          }
        ]
      }
    ]
  },
  {
    path: '/GIS',
    component: Layout,
    name: 'gis',
    redirect: '/GIS/OneMap',
    meta: {
      title: 'GIS管理',
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'OneMap',
        component: () => import('@/views/GIS/OneMap/index.vue'),
        name: 'onemap',
        meta: {
          title: '一图可视',
          noCache: true
        }
      }
    ]
  },
  {
    path: '/SCADA',
    component: Layout,
    name: 'scada',
    // redirect: '/SCADA/Dashboard',
    meta: {
      title: 'SCADA',
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'DTUTable',
        component: () => import('@/views/SCADA/DTUTable.vue'),
        name: 'dtutable',
        meta: {
          title: 'DTU表格数据',
          noCache: true
        }
      },
      {
        path: 'Report',
        component: Layout,
        name: 'report',
        meta: {
          title: '报表统计',
          noCache: true
        },
        children: [
          {
            path: 'DayReport',
            component: () => import('@/views/SCADA/DayReport.vue'),
            name: 'dayreport',
            meta: {
              title: '日报表统计'
            }
          },
          {
            path: 'MonthReport',
            component: () => import('@/views/SCADA/MonthReport.vue'),
            name: 'monthreport',
            meta: {
              title: '月报表统计'
            }
          }
        ]
      },
      {
        path: 'GasTotal',
        component: () => import('@/views/SCADA/GasTotal.vue'),
        name: 'gastotal',
        meta: {
          title: '用气统计',
          noCache: true
        }
      },
      {
        path: 'History',
        component: Layout,
        name: 'history',
        meta: {
          title: '历史查询',
          noCache: true
        },
        children: [
          {
            path: 'DataHistory',
            component: () => import('@/views/SCADA/DataHistory.vue'),
            name: 'DataHistory',
            meta: {
              title: '历史数据查询'
            }
          },
          {
            path: 'AlertHistory',
            component: () => import('@/views/SCADA/AlertHistory.vue'),
            name: 'AlertHistory',
            meta: {
              title: '告警数据查询'
            }
          }
        ]
      },
      {
        path: 'AlertProcess',
        component: () => import('@/views/SCADA/AlertProcess.vue'),
        name: 'alertprocess',
        meta: {
          title: '告警处理',
          noCache: true
        }
      },
      {
        path: 'SetMarker',
        component: () => import('@/views/SCADA/SetMarker/Marker.vue'),
        name: 'setmarker',
        meta: {
          title: '站点配置',
          noCache: true
        }
      },
      {
        path: 'SetDetector',
        component: () => import('@/views/SCADA/SetMarker/Detector.vue'),
        name: 'setdetector',
        meta: {
          title: t('附加设备配置'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/SCADA/SetMarker'
        }
      }
    ]
  },
  {
    path: '/Emergency',
    component: Layout,
    name: 'Emergency',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Emergency/index.vue'),
        name: 'EmergencyIndex',
        meta: {
          title: '应急处置管理',
          icon: 'vi-cib:telegram-plane'
        }
      }
    ]
  },
  {
    path: '/authorization',
    component: Layout,
    redirect: '/authorization/user',
    name: 'Authorization',
    meta: {
      title: t('router.authorization'),
      icon: 'vi-eos-icons:role-binding',
      alwaysShow: true
    },
    children: [
      {
        path: 'department',
        component: () => import('@/views/Authorization/Department/Department.vue'),
        name: 'Department',
        meta: {
          title: '部门管理'
        }
      },
      {
        path: 'user',
        component: () => import('@/views/Authorization/User/User.vue'),
        name: 'User',
        meta: {
          title: '用户管理'
        }
      },
      {
        path: 'menu',
        component: () => import('@/views/Authorization/Menu/Menu.vue'),
        name: 'Menu',
        meta: {
          title: '菜单管理'
        }
      },
      {
        path: 'role',
        component: () => import('@/views/Authorization/Role/Role.vue'),
        name: 'Role',
        meta: {
          title: '角色管理'
        }
      },
      {
        path: 'post',
        component: () => import('@/views/Authorization/Post/post.vue'),
        name: 'Post',
        meta: {
          title: '岗位管理'
        }
      },
      {
        path: 'authrole',
        component: () => import('@/views/Authorization/Role/components/AuthUser.vue'),
        name: 'AuthRole',
        meta: {
          title: t('角色授权用户'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/authorization/role'
        }
      },
      {
        path: 'dictionary',
        component: () => import('@/views/Authorization/Dictionary/index.vue'),
        name: 'Dictionary',
        meta: {
          title: t('字典管理')
        }
      },
      {
        path: 'dictData',
        component: () => import('@/views/Authorization/Dictionary/DictDataList.vue'),
        name: 'dictData',
        meta: {
          title: t('字典数据管理'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          canTo: true,
          activeMenu: '/authorization/dictionary'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})
export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
